<?php
include_once("tpl.php");
$main = htmlBody();
echo tpl($main);
?>
<?php
function htmlBody()	{
	include_once("inc/checkArg.php");
	$mod = checkArg("mod","twoball");
	$lang_select= checkArg("lang","cs");
	include_once("lang/".$lang_select.".php");
	$lang = lang();
	include_once("inc/xml2mod.php");
	$aModList = xml2mod('lotteryMod/mod.xml');
	$sHtml = "";
	foreach($aModList as $aMod)	{
		include_once("inc/mod2ahref.php");
		$sHtml.= mod2ahref($aMod,"intro.php");
		$sHtml.=" ";
	}
	$sHtml.= "<hr />";	
	foreach($aModList as $aMod)	{
		$sHtml.= mod2html($aMod,$mod,$lang);
	}
	return $sHtml;
}
?>
<?php
function mod2html($aMod,$mod,$lang)	{
	$sHtml = "";
	if(strcmp($aMod["file"],$mod)!=0)	{
		return $sHtml;
	}
	else	{
		include_once("inc/mod2ahref.php");
		$sHtml.= mod2ahref($aMod,"history.php")."<br />";
		$aRule = showRule($aMod);
		if($aRule)	{
			$sHtml.=rule2Text($aRule,$aMod["file"],$lang);
		}
		$sHtml.="<hr />";
		return $sHtml;
	}
}
?>
<?php
function rule2Text($aRule,$mod,$lang)	{
	$sText = "";
	$sText.= "<b>".$lang["Rule"]."</b>"."<br />";
	$sText.= $lang["DivideTo"]." ".$aRule["sector"]." ".$lang["Sector"]."<br />";
	include_once("inc/factorialPC.php");
	foreach($aRule["amount"] as $key => $amount)	{
		$sText.=$lang["Sector"]." ".($key+1)." ".$lang["has"]." ".$amount." ".$lang["number"]." .范围系：".$aRule["range"][$key][0]."到".$aRule["range"][$key][1]."&nbsp;&nbsp;&nbsp;";
		$num_Range	= $aRule["range"][$key][1]-$aRule["range"][$key][0]+1;
		$num_Range	= (int)$num_Range;
		$amount		= (int)$amount;
		$proability[$key]	= C($num_Range,$amount);
		$sText.= "有 C(".$num_Range.",".$amount.")".$proability[$key]."种可能"."<br />";
	}
	$proX	=	1;
	foreach($proability as $pro)	{
		$proX*=$pro;
		$sText.= $pro."*";
	}
	$sText.="=";
	$sText.="共有".$proX."种可能"."<br />";
	if(file_exists("bingo/".$mod.".csv"))	{
		$sText.=bingo2Text($mod,$aRule,$proX,$lang);
	}
	return $sText;
}
?>
<?php
function bingo2Text($mod,$aRule,$proX,$lang)	{
	$sText="";
	$sText.= "<b>".$lang["Rate"]."</b>"."<br />";
	include_once("inc/getBingoLvl.php");
	$aBingoLvl	=	getBingoLvl("bingo/".$mod.".csv");
	include_once("inc/getPrizeLvl.php");
	if(file_exists("prize/".$mod.".csv"))	{
		$aPrizeLvl	=	getPrizeLvl("prize/".$mod.".csv");
	}
	$iLost = $proX;
	foreach($aBingoLvl as $key => $bingoLvl)	{
		$sText.= $key.":";
		if(isset($aPrizeLvl))	{
			$sText.= $aPrizeLvl[$key];
		}
		$sText.= "<br />";
		$iZ=0;
		foreach($bingoLvl as $singleLvl)	{
			$iY=1;
			for($i=0;$i<$aRule["sector"];$i++)	{
				if(isset($aRule["bonus"]))	{
					$bonus = (int)$singleLvl["bonus"];
					$bingo = (int)$singleLvl["amount"];
				}
				else	{
					$bingo = (int)$singleLvl[$i];
				}
				if(isset($aRule["select"][$i]))	{
					$select= (int)$aRule["select"][$i];
				}
				else	{
					$select= (int)$aRule["amount"][$i];
				}
			
				$range = (int)$aRule["range"][$i][1]-(int)$aRule["range"][$i][0]+1;
				
				if(!isset($aRule["bonus"]))	{
					$sText.= $bingo."/".$select."/".$range." ";
					$iX=C($range-$select,$select-$bingo);
					$iY*=$iX;
					$sText.= "C(".($range-$select).",".($select-$bingo).")=".$iX." ";
				}
				else	{
					$sText.= $bingo."/".$select."/".$range." ";
					$sText.= $bonus."/".$aRule["bonus"][$i]." ";
					$iX=C($select,$bingo);
					$iY*=$iX;
					$sText.= "C(".$select.",".($bingo).")=".$iX." ";
				
					$iB=C($range-$select-$aRule["bonus"][$i],$aRule["bonus"][$i]-$bonus+$aRule["amount"][$i]-$bingo-1);
					$iY*=$iB;
					$sText.= "*C(".($range-$select-$aRule["bonus"][$i]).",".($aRule["bonus"][$i]-$bonus+$aRule["amount"][$i]-$bingo-1).")=".$iB." ";
				}
			}
			$sText.= "=".$iY."<br />";
			$iZ+=$iY;
		}
		$pro	=	(float)($iZ/$proX*100);
		$sText.= $iZ."/".$proX."=".$pro."% = 1/".($proX/$iZ)."<hr />";
		$iLost -= $iZ;
	}
	$sText.= "Loser:".$iLost."/".$proX."=".(float)($iLost/$proX*100)."%<hr />";
	return $sText;
}
?>
<?php
function showRule($aMod)	{
	$file = 'lotteryMod/'.$aMod["file"].'.csv';
	if(file_exists($file))	{
		include_once("inc/lotteryMod.php");
		$aRule = lotteryMod($file);
		return $aRule;
	}
	else	{
		return False;
	}
}
?>